package com.example.fitapp.activity.activity;
import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.example.fitapp.activity.R;
import com.example.fitapp.activity.http.util.ApiUtil;
import com.example.fitapp.activity.http.util.OkHttpUtil;
import com.example.fitapp.activity.http.util.ApiListener;
import com.example.fitapp.activity.http.api.user.UserLogin;
import com.example.fitapp.activity.utils.L;

import org.json.JSONException;
import org.json.JSONObject;

import okhttp3.OkHttpClient;

public class LoginActivity extends AppCompatActivity implements View.OnClickListener {
    private String emailText ;
    private String passwordText;
    private EditText email;
    private EditText password;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        Button login_button = (Button) findViewById(R.id.button_login);
        TextView register_button = (TextView) findViewById(R.id.button_register);
        email = (EditText) findViewById(R.id.edit_email);
        password = (EditText) findViewById(R.id.edit_password);
        login_button.setOnClickListener(this);
        register_button.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {

        emailText = email.getText().toString();
        passwordText = password.getText().toString();
        switch (v.getId()){
            case R.id.button_login:
                if(emailText.length()<1){
                    Toast.makeText(LoginActivity.this,"请输入邮箱",Toast.LENGTH_SHORT).show();
                    return;
                }
                if(passwordText.length()<1){
                    Toast.makeText(LoginActivity.this,"请输入密码",Toast.LENGTH_SHORT).show();
                    return;
                }
                snedLogin(emailText,passwordText);
                break ;
            case R.id.button_register:
                Intent intent = new Intent(LoginActivity.this, RegisteredActivity.class);
                startActivity(intent);
                break;
            default:
                break;
        }
    }
    private void snedLogin(String email,String password){
        L.d("snedLogin");
        new UserLogin(email,password).post(new ApiListener() {
            @Override
            public void success(ApiUtil apiUtil) {
                UserLogin api = (UserLogin)apiUtil;
                String response = api.mResponse;
                Log.i("Login","Success");
                try {
                    JSONObject responseObj = new JSONObject(response);
                    String data = responseObj.get("data").toString() ;
                    JSONObject responseData = new JSONObject(data);
                    String token = responseData.get("token").toString();
                    System.out.println(token);
                    SharedPreferences.Editor editor = getSharedPreferences("data",MODE_PRIVATE).edit();
                    editor.putString("token",token);
                    editor.apply();
                    Intent intent = new Intent(LoginActivity.this, MainActivityWithTap.class);
                    startActivity(intent);
                }catch (JSONException e){
                    e.printStackTrace();
                }


            }

            @Override
            public void failure(ApiUtil apiUtil) {
                Log.e("Login","请求失败");
//                UserLogin api = (UserLogin)apiUtil;
//                String response = api.mResponse;
//                try {
//                    JSONObject responseObj = new JSONObject(response);
////                    System.out.println(responseObj.get("msg"));
//                    String msg = responseObj.get("msg").toString() ;
//                    Toast.makeText(LoginActivity.this,msg,Toast.LENGTH_SHORT).show();
//                }catch (JSONException e){
//                    e.printStackTrace();
//                }
            }
        });

    }
}
